package com.ambow.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.ambow.Entity.TextBook;
import com.ambow.utils.HibernateUtil;

public class BookDao {
	public boolean addBook(String new_isbn, String new_name, String new_author,String new_edition,
			String new_publish, String date, double new_price,String new_remark ) throws ParseException{
       
	    Configuration conf = new Configuration().configure();
		SessionFactory sf=conf.buildSessionFactory();
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		
		TextBook tb = new TextBook();
		
		tb.setISBN(new_isbn);
		tb.setName(new_name);
		tb.setAuthor(new_author);
		tb.setRevision(new_edition);
		tb.setPublish(new_publish);
		tb.setPublishTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
		tb.setPrice(new_price);
		tb.setRemark(new_remark);
		
		session.save(tb);
		session.persist(tb);
		
		tx.commit();
		session.close();
		sf.close();
		
		return true;
		    
	}

	public boolean updateinfo(int id, String isbn, String name, String author,String version,String publish,String date,double price,String remark){
		boolean f=false;
		try {
//		    int id=1001;
//		    String postion="老师";
//		    String remark="你好";
	        HibernateUtil hibernateUtil = new HibernateUtil();
		    Session session = hibernateUtil.getSession();
		    session.beginTransaction();
		    Query query = session.createQuery("update TextBook t set t.ISBN='"+isbn +"', t.name='"+name+"',t.author='"+author+"',t.revision='"+version+"',t.publish='"+publish+"',t.publishTime='"+date+"',t.price="+price+",t.remark='"+remark+"' where t.id="+id);
		    query.executeUpdate();
		    session.getTransaction().commit();
		    
		    f=true;
		 }
	    catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
		return f;
	}
	public List bookQuery(String name,String author,String publish){
		List<TextBook> list=null;
//		String role="学生管理员";
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.getSession();
		
		//from TextBook where name like ? and author like ? and publish like ?
		//from TextBook t where t.book_name='"+name+"' and t.author='"+author+"' and t.publish='"+publish+"' 
		Query query = session.createQuery("from TextBook where name like ? and author like ? and publish like ?");
		   query.setString(0,name);
           query.setString(1, author);
           query.setString(2,publish);
		list=query.list();
		
		
		return list;
	}
	public List allbookQuery(){
		List list=null;
//		String role="学生管理员";
		HibernateUtil hibernateUtil = new HibernateUtil();
	    Session session = hibernateUtil.getSession();

		Query query = session.createQuery("from TextBook");
		list=query.list();
		if(list!=null){
			return list;
		}
		session.getTransaction().commit();
		
		return list;
	}
	@Test
		public boolean deleteBook(int id){

			HibernateUtil hibernateUtil = new HibernateUtil();
			Session session = hibernateUtil.sessionFactory.openSession();
//			Session session = hibernateUtil.getSession();
			session.beginTransaction();
			
			Query query = session.createSQLQuery("delete from TextBook where id="+id+"");
			query.executeUpdate();
			session.getTransaction().commit();
			session.close();
		    return true;
		
			
		}
		
	
}
